using System;
using System.Collections.Generic;
using System.Windows.Forms;
using FarPoint.Win.Spread;
using FarPoint.Win.Spread.Model;
using Spread.Common.DataStore;
using Spread.Common.DataStore.Models;

namespace Spread.Outline.Features
{
    public partial class RangeGroupDemo : Spread.Common.Features.Sample.BaseSampleControl
    {
        private JsonDataSource _dataSource;

        public RangeGroupDemo()
        {
            InitializeComponent();
            Name = "Outlines, Row and Column Range Groups";

            _dataSource = new DataService().JsonDataSource;

            InitSpreadSheet(fpSpread.ActiveSheet);

            btnGroup.Click += BtnGroup_Click;
            btnUngroup.Click += BtnUngroup_Click;
            btnShowDetail.Click += BtnShowDetail_Click;
            btnHideDetail.Click += BtnHideDetail_Click;
            chkSummaryBelow.CheckedChanged += ChkSummaryBelow_CheckedChanged;
            chkSummaryRight.CheckedChanged += ChkSummaryRight_CheckedChanged;
        }

        private void InitSpreadSheet(SheetView sheet)
        {
            List<MovieRecord> movieRecords = _dataSource.GetMovieRecords();
            sheet.DataSource = movieRecords;

            sheet.AddRangeGroup(1, 10, true);
            sheet.AddRangeGroup(1, 4, true);
            sheet.AddRangeGroup(6, 4, true);
            sheet.AddRangeGroup(1, 3, false);
            sheet.AddRangeGroup(1, 2, false);
            
            for (int col = 0; col < sheet.ColumnCount; ++col)
                sheet.Columns[col].Width = sheet.GetPreferredColumnWidth(col, PreferredSizeColumnOptions.IncludeAll);
        }

        #region Event handlers
        private void BtnGroup_Click(object sender, EventArgs e)
        {
            try
            {
                SheetView sheet = fpSpread.ActiveSheet;
                CellRange cr = sheet.GetSelection(0);
                if (cr.Column == -1 && cr.Row == -1) // sheet selection
                {

                }
                else if (cr.Column == -1) // row selection
                {
                    sheet.AddRangeGroup(cr.Row, cr.RowCount, true);
                }
                else if (cr.Row == -1) // column selection
                {
                    sheet.AddRangeGroup(cr.Column, cr.ColumnCount, false);
                }
                else // cell range selection
                {
                    MessageBox.Show("Please select row or column for group");

                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }

        }

        private void BtnUngroup_Click(object sender, EventArgs e)
        {
            try
            {
                SheetView sheet = fpSpread.ActiveSheet;
                CellRange cr = sheet.GetSelection(0);
                if (cr.Column == -1 && cr.Row == -1) // sheet selection
                {

                }
                else if (cr.Column == -1) // row selection
                {
                    sheet.RemoveRangeGroup(cr.Row, cr.RowCount, true);
                }
                else if (cr.Row == -1) // column selection
                {
                    sheet.RemoveRangeGroup(cr.Column, cr.ColumnCount, false);
                }
                else // cell range selection
                {
                    MessageBox.Show("Please select row or column which are in a group");

                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }

        private void BtnShowDetail_Click(object sender, EventArgs e)
        {
            try
            {
                SheetView sheet = fpSpread.Sheets[0];
                CellRange cr = sheet.GetSelection(0);
                if (cr.Column == -1 && cr.Row == -1) // sheet selection
                {

                }
                else if (cr.Column == -1) // row selection
                {
                    for (int i = cr.Row; i < cr.Row + cr.RowCount; i++)
                    {
                        int level = sheet.GetRangeGroupLevels(true);
                        for (int j = 0; j < level; j++)
                        {
                            RangeGroupInfo[] groupInfo = sheet.GetRangeGroupInfo(j, true);
                            if (groupInfo != null)
                            {
                                foreach (RangeGroupInfo rgi in groupInfo)
                                    if (i >= rgi.Start && i <= rgi.End)
                                        sheet.ExpandRangeGroup(rgi, true, true);
                            }
                        }
                    }
                }
                else if (cr.Row == -1) // column selection
                {
                    for (int i = cr.Column; i < cr.Column + cr.ColumnCount; i++)
                    {
                        int level = sheet.GetRangeGroupLevels(true);
                        for (int j = 0; j < level; j++)
                        {
                            RangeGroupInfo[] groupInfo = sheet.GetRangeGroupInfo(j, false);
                            if (groupInfo != null)
                            {
                                foreach (RangeGroupInfo rgi in groupInfo)
                                    if (i >= rgi.Start && i <= rgi.End)
                                        sheet.ExpandRangeGroup(rgi, false, true);
                            }
                        }
                    }
                }
                else // cell range selection
                {
                    MessageBox.Show("Please select row or column which are in a group");

                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }

        }

        private void BtnHideDetail_Click(object sender, EventArgs e)
        {
            try
            {
                SheetView sheet = fpSpread.Sheets[0];
                CellRange cr = sheet.GetSelection(0);
                if (cr.Column == -1 && cr.Row == -1) // sheet selection
                {

                }
                else if (cr.Column == -1) // row selection
                {
                    for (int i = cr.Row; i < cr.Row + cr.RowCount; i++)
                    {
                        int level = sheet.GetRangeGroupLevels(true);
                        for (int j = 0; j < level; j++)
                        {
                            RangeGroupInfo[] groupInfo = sheet.GetRangeGroupInfo(j, true);
                            if (groupInfo != null)
                            {
                                foreach (RangeGroupInfo rgi in groupInfo)
                                    if (i >= rgi.Start && i <= rgi.End)
                                        sheet.ExpandRangeGroup(rgi, true, false);
                            }
                        }
                    }
                }
                else if (cr.Row == -1) // column selection
                {
                    for (int i = cr.Column; i < cr.Column + cr.ColumnCount; i++)
                    {
                        int level = sheet.GetRangeGroupLevels(true);
                        for (int j = 0; j < level; j++)
                        {
                            RangeGroupInfo[] groupInfo = sheet.GetRangeGroupInfo(j, false);
                            if (groupInfo != null)
                            {
                                foreach (RangeGroupInfo rgi in groupInfo)
                                    if (i >= rgi.Start && i <= rgi.End)
                                        sheet.ExpandRangeGroup(rgi, false, false);
                            }
                        }
                    }
                }
                else // cell range selection
                {
                    MessageBox.Show("Please select row or column which are in a group");
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }

        }

        private void ChkSummaryBelow_CheckedChanged(object sender, EventArgs e)
        {
            fpSpread.ActiveSheet.RangeGroupSummaryRowBelow = chkSummaryBelow.Checked;
        }

        private void ChkSummaryRight_CheckedChanged(object sender, EventArgs e)
        {
            fpSpread.ActiveSheet.RangeGroupSummaryColumnRight = chkSummaryRight.Checked;
        }
        #endregion
    }
}


//
//RangeGroupDemo.Designer.cs
//


namespace Spread.Outline.Features
{
    partial class RangeGroupDemo
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.fpSpread = new FarPoint.Win.Spread.FpSpread();
            this.fpSpread_Sheet1 = new FarPoint.Win.Spread.SheetView();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.btnGroup = new System.Windows.Forms.Button();
            this.btnUngroup = new System.Windows.Forms.Button();
            this.btnShowDetail = new System.Windows.Forms.Button();
            this.btnHideDetail = new System.Windows.Forms.Button();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.chkSummaryBelow = new System.Windows.Forms.CheckBox();
            this.chkSummaryRight = new System.Windows.Forms.CheckBox();
            this.pnlMain.SuspendLayout();
            this.pnlControls.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).BeginInit();
            this.groupBox1.SuspendLayout();
            this.tableLayoutPanel1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.fpSpread);
            // 
            // pnlControls
            // 
            this.pnlControls.Controls.Add(this.groupBox2);
            this.pnlControls.Controls.Add(this.groupBox1);
            // 
            // fpSpread
            // 
            this.fpSpread.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread.Location = new System.Drawing.Point(0, 0);
            this.fpSpread.Name = "fpSpread";
            this.fpSpread.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.fpSpread_Sheet1});
            this.fpSpread.Size = new System.Drawing.Size(477, 487);
            this.fpSpread.Skin = FarPoint.Win.Spread.DefaultSpreadSkins.Office2016White;
            this.fpSpread.StatusBarVisible = true;
            this.fpSpread.TabIndex = 0;
            // 
            // fpSpread_Sheet1
            // 
            this.fpSpread_Sheet1.Reset();
            this.fpSpread_Sheet1.SheetName = "Sheet1";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.tableLayoutPanel1);
            this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
            this.groupBox1.Location = new System.Drawing.Point(0, 0);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(157, 89);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Actions and Settings";
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.ColumnCount = 2;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.Controls.Add(this.btnUngroup, 1, 0);
            this.tableLayoutPanel1.Controls.Add(this.btnGroup, 0, 0);
            this.tableLayoutPanel1.Controls.Add(this.btnShowDetail, 0, 2);
            this.tableLayoutPanel1.Controls.Add(this.btnHideDetail, 1, 2);
            this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tableLayoutPanel1.Location = new System.Drawing.Point(3, 16);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 3;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 30F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 10F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 30F));
            this.tableLayoutPanel1.Size = new System.Drawing.Size(151, 70);
            this.tableLayoutPanel1.TabIndex = 0;
            // 
            // btnGroup
            // 
            this.btnGroup.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btnGroup.Location = new System.Drawing.Point(3, 3);
            this.btnGroup.Name = "btnGroup";
            this.btnGroup.Size = new System.Drawing.Size(69, 24);
            this.btnGroup.TabIndex = 0;
            this.btnGroup.Text = "Group";
            this.btnGroup.UseVisualStyleBackColor = true;
            // 
            // btnUngroup
            // 
            this.btnUngroup.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btnUngroup.Location = new System.Drawing.Point(78, 3);
            this.btnUngroup.Name = "btnUngroup";
            this.btnUngroup.Size = new System.Drawing.Size(70, 24);
            this.btnUngroup.TabIndex = 1;
            this.btnUngroup.Text = "Ungroup";
            this.btnUngroup.UseVisualStyleBackColor = true;
            // 
            // btnShowDetail
            // 
            this.btnShowDetail.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btnShowDetail.Location = new System.Drawing.Point(3, 43);
            this.btnShowDetail.Name = "btnShowDetail";
            this.btnShowDetail.Size = new System.Drawing.Size(69, 24);
            this.btnShowDetail.TabIndex = 2;
            this.btnShowDetail.Text = "Show Detail";
            this.btnShowDetail.UseVisualStyleBackColor = true;
            // 
            // btnHideDetail
            // 
            this.btnHideDetail.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btnHideDetail.Location = new System.Drawing.Point(78, 43);
            this.btnHideDetail.Name = "btnHideDetail";
            this.btnHideDetail.Size = new System.Drawing.Size(70, 24);
            this.btnHideDetail.TabIndex = 3;
            this.btnHideDetail.Text = "Hide Detail";
            this.btnHideDetail.UseVisualStyleBackColor = true;
            // 
            // groupBox2
            // 
            this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.groupBox2.Controls.Add(this.chkSummaryRight);
            this.groupBox2.Controls.Add(this.chkSummaryBelow);
            this.groupBox2.Location = new System.Drawing.Point(0, 107);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(157, 89);
            this.groupBox2.TabIndex = 1;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Direction";
            // 
            // chkSummaryBelow
            // 
            this.chkSummaryBelow.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.chkSummaryBelow.Location = new System.Drawing.Point(6, 19);
            this.chkSummaryBelow.Name = "chkSummaryBelow";
            this.chkSummaryBelow.Size = new System.Drawing.Size(148, 30);
            this.chkSummaryBelow.TabIndex = 0;
            this.chkSummaryBelow.Text = "Summary rows below detail";
            this.chkSummaryBelow.UseVisualStyleBackColor = true;
            // 
            // chkSummaryRight
            // 
            this.chkSummaryRight.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.chkSummaryRight.Location = new System.Drawing.Point(6, 47);
            this.chkSummaryRight.Name = "chkSummaryRight";
            this.chkSummaryRight.Size = new System.Drawing.Size(153, 31);
            this.chkSummaryRight.TabIndex = 1;
            this.chkSummaryRight.Text = "Summary columns to right of detail";
            this.chkSummaryRight.UseVisualStyleBackColor = true;
            // 
            // RangeGroupDemo
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "RangeGroupDemo";
            this.pnlMain.ResumeLayout(false);
            this.pnlControls.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).EndInit();
            this.groupBox1.ResumeLayout(false);
            this.tableLayoutPanel1.ResumeLayout(false);
            this.groupBox2.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private FarPoint.Win.Spread.FpSpread fpSpread;
        private FarPoint.Win.Spread.SheetView fpSpread_Sheet1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
        private System.Windows.Forms.Button btnUngroup;
        private System.Windows.Forms.Button btnGroup;
        private System.Windows.Forms.Button btnShowDetail;
        private System.Windows.Forms.Button btnHideDetail;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.CheckBox chkSummaryRight;
        private System.Windows.Forms.CheckBox chkSummaryBelow;
    }
}
